﻿using System.Collections.Generic;
using System.Web.Mvc;
using Business;
using DAL;
using Blog.Models;

namespace Blog.Controllers
{
    public class CategoriesController : Controller
    {
        //
        // GET: /Categories/

        public ActionResult Index()
        {
            List<Category> categories = new List<Category>();
            categories = CategoriesManager.Read();

            ViewBag.Message = "Naviguez dans la liste des catégories";

            return View(categories);
        }

        //
        // GET: /Categories/Details/5

        public ActionResult Details(int id)
        {
            Category category = new Category();
            category = CategoriesManager.Read(id);

            ViewBag.Message = "Informations sur cette catégorie";

            return View(category);
        }

        //
        // GET: /Categories/Create

        [Authorize]
        public ActionResult Create()
        {
            ViewBag.Message = "Créez votre catégorie";

            return View();
        } 

        //
        // POST: /Categories/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(CategoryCreateViewModel category)
        {
            if (ModelState.IsValid)
            {
                Category c = new Category();
                c.Name = category.Name;
                c.Description = category.Description;

                CategoriesManager.Create(c);

                return RedirectToAction("Index");
            }
            else
                return View();
        }
        
        //
        // GET: /Categories/Edit/5
        
        [Authorize]
        public ActionResult Edit(int id)
        {
            Category category = new Category();
            category = CategoriesManager.Read(id);

            CategoryCreateViewModel c = new CategoryCreateViewModel();
            c.ID = category.ID;
            c.Name = category.Name;
            c.Description = category.Description;

            ViewBag.Message = "Modifiez les informations";

            return View(c);
        }

        //
        // POST: /Categories/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Edit(CategoryCreateViewModel category)
        {
            if (ModelState.IsValid)
            {
                Category c = new Category();
                c.ID = category.ID;
                c.Name = category.Name;
                c.Description = category.Description;

                CategoriesManager.Update(c);
                string url = Request.ServerVariables["HTTP_REFERER"];
                string[] urlArray = url.Split('/');

                return RedirectToAction("Details/" + urlArray[5], "Categories");
            }
            else
                return View();
        }

        //
        // GET: /Categories/Delete/5
        
        [Authorize]
        public ActionResult Delete(int id)
        {
            CategoriesManager.Delete(id);

            return RedirectToAction("Index");
        }
    }
}
